1. Field of the Invention
The present invention relates to the field of computer programming languages, and in particular to a method and apparatus for extending a program element in a dynamically typed programming language.
Sun, Sun Microsystems, the Sun logo, Solaris and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. All SPARC trademarks are used under license and are trademarks of SPARC International, Inc. in the United States and other countries. Products bearing SPARC trademarks are based upon an architecture developed by Sun Microsystems, Inc.
2. Background Art
A typical computer program is comprised of many program elements. Program elements include functions, classes and enumerations. Program elements are defined by computer code. In prior art systems, once an element is defined, the only way to extend the program element (i.e., add code to the program element) is to modify the source code for the program element. This may not be either desirable or possible if the element is part of a library or a shared repository. This problem can be better understood with a review of program elements.
Program Elements
Program elements are defined using computer code written in a computer programming language. Enumerations are structural elements and are used to define a list of items. For example, in the code below, an enumeration defines a new type, primes_less_than_ten.
enum primes_less_than_ten {    TWO, THREE, FIVE, SEVEN}
Functions are elements that perform calculations or operations in a program. Functions typically have one or more program code statements that are executed when the function is called. For example, in the code below, a function defines a set of calculations which determine N factorial, which is defined as N*(N−1)* . . . *1.
int factorial (int N) {    if(N == 0) {         return 1;     } else {         return N * factorial (N − 1);    }}
A class is a structure that contains variables and functions as members. Typically, a class is defined by listing the member variables and functions according to the structure imposed by the programming language in use. For example, in the code below, a class defines a box data structure which has variables length, width and height as well as member functions volume and surface_area.
class box {int length, width, height;int volume( ) {         return length * width * height;    }int surface_area( ) {         return 2 * (length * width + length * height + width * height);     } }
Extending Program Elements
In some cases, it may be desirable to extend (i.e., add to) a program element. For example, in the enumeration above, a programmer may wish to include the member ONE in primes_less_than_ten. Similarly, a programmer may wish to track the number of recursive calls to factorial by incrementing a counter and printing the counter to the screen each time a recursive call is made. A programmer may wish to extend the above “box” class by adding a variable to represent how many items are placed in the box.
In prior art programming languages, a programmer must modify the source code to extend a program element. In the above examples, the source code becomes:
enum primes_less_than_ten {    ONE, TWO, THREE, FIVE, SEVEN}int factorial (int N) {    if (N== 0) {counter = counter + 1;// counter has been declared before this// section of code is executed, and is// initialized to 0        cout << counter << “ ”;return 1;   }else {counter = counter + 1; // counter has been declared before this// section of code is executed, and is// initialized to 0      cout << counter << “ ”return N * factorial (N − 1);  }}class box {     int length, width, height, number_in_box;int volume( ) {          return length * width * height;     }int surface_area( ) {          return 2 * (length * width + length * height + width * height);     } }
However, sometimes, it is not desirable or even possible for a programmer to modify the source code used to create a program element. For example, a program element may be defined in a library which the programmer is unable to modify. In another example, the program element is defined in a shared repository of program code. Altering the program element's source code could result in adverse consequences to other programs that use the shared repository to define the program element.